#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update:


'''回答问题，绝对坐标
'''

__revision__ = '0.1'
__author__ = 'lxd'
import questionDB

class Helper():
    def __init__(self,log,dm):
        self.log = log
        self.dm = dm
        self.db = questionDB.QuestionDB(self.log, r'static/question.db')

    def canAnswer(self):
        str = self.dm.ocr(106,-13,329,11,'00ff00-000000')
        if str == u'本日答题次数已用完，请明日再答':
            self.log.debug(u'本日答题次数已用完，请明日再答')
            return False
        else:
            return True

    def clickSuperQuestion(self):
        if self.dm.getColor(-25,30) == 'ffffff':
            self.dm.click(312,36)
            self.log.debug('click super question success')

    def clickNext(self):
        self.dm.click(317,-13)

    def answerIsRight(self):
        #首先判断是否等于 -1，这时不要记录
        if self.dm.ocr(90,-24,275,0, 'ffff00') == u'恭喜你答对了。':
            return True
        elif self.dm.findStr(u'正确答案', 91,-25,265,-1, 'ff0000'):
            return False
        return  -1

    def clickA(self):
        self.dm.click(213,-87)

    def clickB(self):
        self.dm.click(203,-65)

    def clickC(self):
        self.dm.click(217,-42)

    def clickAnswer(self):
        """点击正确项
        """
        if self.answer == self.answer_a:
            self.clickA()
        elif self.answer == self.answer_b:
            self.clickB()
        elif self.answer == self.answer_c:
            self.clickC()
        else:
            self.log.error(u'%s is not in [%s, %s, %s], so can not click' %(self.answer, self.answer_a, self.answer_b, self.answer_c))

    def getQuestionAndAnswers(self):
        """得到题目和选项
        """
        self.question = self.dm.ocr(102,-156,346,-134)
        self.answer_a = self.dm.ocr(102,-97,361,-78)
        self.answer_b = self.dm.ocr(101,-75,356,-54)
        self.answer_c = self.dm.ocr(102,-53,365,-32)
        self.log.debug('%s#%s, %s, %s' %(self.question, self.answer_a, self.answer_b, self.answer_c))

    def getAnswer(self):
        """得到正确答案
        """
        return self.dm.ocr(144,-24,157,0, 'ff0000-000000')

    def processAnswer(self): 
        self.answer = self.db.selectData(self.question, self.answer_a, self.answer_b, self.answer_c)
        if self.answer:
            self.clickAnswer()
        else:
            #默认选A
            self.answer = self.answer_a
            self.clickA()

    def checkAnswer(self):
        status = self.answerIsRight()
        if status != -1:
            if not status:
                answer_id = self.getAnswer()
                self.log.debug(answer_id)
                if answer_id == 'A':
                    self.answer = self.answer_a
                elif answer_id == 'B':
                    self.answer = self.answer_b
                elif answer_id == 'C':
                    self.answer = self.answer_c
                else:
                    self.answer = ''
            if self.question != '' and self.answer != '':
                self.db.insertData(self.question, self.answer)
            return True
        else:
            return False

